<template>

	<div class="wrapper">

		<!--header部分-->
		<header>
			<p>用户登录</p>
		</header>

		<!--表单部分-->
		<ul class="form-box">
			<li>
				<div class="title">
					手机号码:
				</div>
				<div class="content">
					<!--input表示接收输入，type表示接收类型，接收类型是text，即任何文本-->
					<!--placeholder表示占位符-->
					<input type="text" v-model="userId" placeholder="手机号码">
				</div>
			</li>
			<li>
				<div class="title">
					密码：
				</div>
				<div class="content">
					<input type="password" v-model="password" placeholder="密码">
				</div>
			</li>
		</ul>
		<div class="button-login">
			<button @click="LoginPage()">登录</button>
			<!-- <button v-if="!isVerificationPassed" :disabled="!isVerificationPassed" @click="LoginPage()">登录</button> -->
		</div>
		<div class="button-register">
			<button @click="RegisterPage">去注册</button>
		</div>
		<!-- <button @click="onShow">开始验证</button> -->
		<Vcode :show="isShow" @success="onSuccess" @close="onClose" />
		<!--底部菜单部分-->
		<FooterPage>

		</FooterPage>
	</div>
</template>

<script>
	import { ref } from 'vue';
	import FooterPage from '../components/FooterPage.vue';
	import Vcode from 'vue3-puzzle-vcode';
	export default {
		name: 'LoginPage',
		data() {
			return {
				// 字符串类型
				userId: '',
				password: '',
				isMember: this.$route.query.isMember,
				isShow: ref(false),
				isVerificationPassed: ref(false)
			}
		},
		mounted() {
			this.showVerification();
		},
		// 非空验证
		methods: {
			onShow() {
				this.isShow = true;
			},
			onClose() {
				this.isShow = false;
			},
			onSuccess() {
				this.isShow = false;
				this.isVerificationPassed = true;
				// this.LoginPage();
			},
			showVerification() {
				if (!this.isVerificationPassed) {
					this.isShow = true;
				}
			},
			isStrongPassword(passwordtest) {
				const uppercaseRegex = /[A-Z]/;
				const lowercaseRegex = /[a-z]/;
				const digitRegex = /[0-9]/;
				const specialCharRegex = /[!@#&*.()]/;
				// 检查密码长度是否大于8位
				const isLengthValid = passwordtest.length > 8;

				return (
					isLengthValid &&
					uppercaseRegex.test(passwordtest) &&
					lowercaseRegex.test(passwordtest) &&
					digitRegex.test(passwordtest) &&
					specialCharRegex.test(passwordtest)
				);
			},
			LoginPage() {
				if (this.userId == '') {
					alert('手机号码不能为空！');
					return;
				}
				// if (this.userId.length != 11 || this.userId.charAt(0) != '1') {
				// 	alert('手机号码格式输入有误！')
				// }
				// if (this.password == '') {
				// 	alert('密码不能为空！')
				// 	return;
				// }
				// if (!this.isStrongPassword(this.password)) {
				// 	alert('密码强度不足！密码应包含至少一个大写字母、一个小写字母、一个数字、一个特殊字符，并且长度大于8位。')
				// 	return;
				// }
				// 登录请求
				
				let url = `UserController/getUserByIdByPass/${this.userId}/${this.password}`;
				this.$axios.get(url).then(response => {
					let user = response.data.result;
					if (user === null || user === '') {
						alert('用户名或密码不正确！');
					} else {
						// SessionStorage有存储限制
						// 为了防止数据溢出,将用户头像移除SessionStorage
						user.userImg = '';
						// SessionStorage存储有限制
						this.$setSessionStorage('user', user);
						// 登陆成功，回退
						if(this.isMember){
							this.$router.push('/userVip');
						}else{
							this.$router.go(-1);
						}
						// this.$router.go(-1);
						
					}
					//判断是否登录
				}).catch(error => {
					console.error(error);
				})
			},
			RegisterPage() {
				this.$router.push({
					path: '/register'
				});
			}

		},
		components: {
			FooterPage,
			Vcode,
		}
	}
</script>

<style scoped>
	/*********总容器************/
	.wrapper {
		height: 100%;
		width: 100%;
	}

	/*********header部分********/
	.wrapper header {
		width: 100%;
		height: 12vw;

		background-color: #0097FF;
		color: #fff;
		font-size: 4.8vw;

		position: fixed;
		left: 0;
		top: 0;
		/* 给予高层叠级别 */
		z-index: 1000;

		/* 位置上下左右居中 */
		display: flex;
		justify-content: center;
		align-items: center;
	}

	/***********表单部分*************/
	.wrapper .form-box {
		width: 100%;
		margin-top: 12vw;
		/* 利用外边距隔开header部分 */
	}

	.wrapper .form-box li {
		box-sizing: border-box;
		padding: 4vw 3vw 0 3vw;
		display: flex;
		align-items: center;
	}

	.wrapper .form-box li .title {
		/* 表示不可伸展、不可收缩，并且初始大小为视口宽度的 18%，这样，改容器在弹性窗口中固定不变 */
		flex: 0 0 18vw;
		font-size: 3vw;
		font-weight: 700;
		color: #666;
	}

	.wrapper .form-box li .content {
		flex: 1;
		/*1代表了伸缩比例*/
	}

	.wrapper .form-box li .content input {
		border: none;
		outline: none;
		width: 100%;
		height: 4vw;
		font-size: 3vw;
	}

	.wrapper .button-login {
		width: 100%;
		box-sizing: border-box;
		padding: 4vw 3vw 0 3vw;
	}

	.wrapper .button-login button {
		width: 100%;
		height: 10vw;

		font-size: 3.8vw;
		font-weight: 700;

		color: #fff;
		background-color: #38CA73;

		border-radius: 4px;

		border: none;
		outline: none;
	}

	.wrapper .button-register {
		width: 100%;
		box-sizing: border-box;
		padding: 4vw 3vw 0 3vw;
	}

	.wrapper .button-register button {
		width: 100%;
		height: 10vw;

		font-size: 3.8vw;
		font-weight: 700;

		color: #666;
		background-color: #EEE;

		border-radius: 4px;

		border: none;
		outline: none;
		border: solid 1px #DDD;
	}
</style>
